package com.micro.edu.controller;

import com.micro.edu.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

    private static final Logger LOG = LoggerFactory.getLogger(EmailController.class);

    @Autowired
    private EmailService emailService;

    @Value("${mail.username:403266439@qq.com}")
    private String username;

    @Value("${mail.message.subject:注册验证码}")
    private String subject;

    @Value("${mail.message.content:你的验证码为：%s ，%d 分钟内有效}")
    private String content;

    @Value("${code.expireTime:10}")
    private Integer expireTime = 10;

    @GetMapping("/{email}/{code}")
    public Integer send(@PathVariable String email, @PathVariable String code) {
        content = String.format(content, code, expireTime);
        LOG.info("邮件信息：from:{} ,to:{} ,subject:{} ,content:{}", username, email, subject, content);
        try {
            emailService.send(username, email, subject, content);
            LOG.info("发送成功~~~~");
            return 1;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return 0;
        }
    }
}
